from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED, as_completed
import time


def spider(page):
    time.sleep(page)
    print(f"crawl task{page} finished")
    return page


with ThreadPoolExecutor(max_workers=5) as executor:
    task1 = executor.submit(spider, 1)
    task2 = executor.submit(spider, 2)
    task3 = executor.submit(spider, 3)
    task4 = executor.submit(spider, 4)
    task5 = executor.submit(spider, 5)
    print("aaa")
    task6 = executor.submit(spider, 6)
    task7 = executor.submit(spider, 7)
    print("bbb")
    wait([task1, task2, task3, task4, task5, task6, task7], return_when=ALL_COMPLETED)
    print("ccc")


# 参数times用来模拟网络请求的时间
def get_html(times):
    time.sleep(times)
    print("get page {}s finished".format(times))
    return times


if __name__ == "__main__":
    executor = ThreadPoolExecutor(max_workers=2)
    urls = [3, 2, 4] # 并不是真的url
    all_task = [executor.submit(get_html, (url)) for url in urls]

    for future in as_completed(all_task):
        data = future.result()
        print("in main: get page {}s success".format(data))




if __name__ == "__main__":
    executor = ThreadPoolExecutor(max_workers=5)
    glo_begin_time = datetime.datetime.strptime("2021-07-08 00:01:00", "%Y-%m-%d %H:%M:%S")
    for day in range(23):
        glo_day_time = glo_begin_time + datetime.timedelta(days=day)
        glo_valid_media = get_history_media(glo_day_time)
        glo_everyday_effect = get_everyday_effect(glo_valid_media, glo_day_time)
        res = []
        for i in range(391, 480):
            print(i)
            glo_report_time = glo_day_time + datetime.timedelta(minutes=(i * 3))
            # valid_media = get_history_media(report_time)
            task = executor.submit(generate_future, (glo_valid_media, glo_everyday_effect, glo_report_time))
            res.append(task)
            # generate_future(glo_valid_media, glo_everyday_effect, glo_report_time)
        wait(res, return_when=ALL_COMPLETED)